package com.seqb.manage.user.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.user.domain.UserSubjectDO;
import com.seqb.manage.user.service.UserSubjectService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lilinshen
 * @title 用户科目表
 * @description 请填写相关描述
 * @date 2019-12-04
 */
@Controller
@RequestMapping("/manage/user/userSubject")
public class UserSubjectController extends BaseController {
    @Autowired
    private UserSubjectService userSubjectService;

    @GetMapping()
    @RequiresPermissions("manage:user:userSubject:list")
    @Log("用户科目表列表")
    String UserSubject() {
        return "manage/user/userSubject.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:user:userSubject:list")
    @Log("用户科目表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<UserSubjectDO> page = userSubjectService.list(query);
        PageUtils pageUtils = new PageUtils(page.getResult(), page.getTotal());
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:user:userSubject:add")
    @Log("用户科目表添加")
    String add() {
        return "manage/user/userSubject.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:user:userSubject:edit")
    @Log("用户科目表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        UserSubjectDO userSubject = userSubjectService.get(id);
        model.addAttribute("userSubject", userSubject);
        return "manage/user/userSubject.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:user:userSubject:add")
    @Log("用户科目表添加保存")
    public ResultVo save(UserSubjectDO userSubject) {
        if (userSubjectService.save(userSubject) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("用户科目表编辑保存")
    @RequiresPermissions("manage:user:userSubject:edit")
    public ResultVo update(UserSubjectDO userSubject) {
        userSubjectService.update(userSubject);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:user:userSubject:remove")
    @Log("用户科目表删除")
    public ResultVo remove(Long id) {
        if (userSubjectService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:user:userSubject:remove")
    @Log("用户科目表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        userSubjectService.batchRemove(ids);
        return ResultVo.ok();
    }

}
